package com.billing.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.billing.model.entity.TAssetsReminding;
import com.billing.service.AssetsRemindingService;

@Component
public class AssetsRemindingTimer {
	@Resource
	private AssetsRemindingService assetsRemindingService;
	
    public void test() {
       System.out.println("111");
    }
	public void AssetsRemindingJob() {
		long currentTime = new Date().getTime();
		List<TAssetsReminding> remindingList = assetsRemindingService.findRemindingList();
	    for (TAssetsReminding assetsReminding : remindingList) {
			if(currentTime > assetsReminding.getEndtime()){
				//提醒失效
				TAssetsReminding reminding = new TAssetsReminding();
				reminding.setStatus(2);
				reminding.setId(assetsReminding.getId());
				assetsRemindingService.updateAssetsReminding(reminding);
			}
		}
	    List<TAssetsReminding> newRemindingList = assetsRemindingService.findRemindingList();
	    for (TAssetsReminding newAssetsReminding : newRemindingList) {
			if(currentTime > newAssetsReminding.getHandleTime()){
				//更新提醒时间
				int intervalType = newAssetsReminding.getRemindingInterval();
				Calendar cal = Calendar.getInstance();
				if(intervalType == 1){
					cal.add(1, cal.MONTH);
				}else if(intervalType == 2){
					cal.add(2, cal.MONTH);
				}else if(intervalType == 3){
					cal.add(3, cal.MONTH);
				}else if(intervalType == 4){
					cal.add(6, cal.MONTH);
				}
				if(cal.getTime().getTime() > newAssetsReminding.getEndtime()){
					//提醒失效
					TAssetsReminding reminding2 = new TAssetsReminding();
					reminding2.setStatus(2);
					reminding2.setId(newAssetsReminding.getId());
					assetsRemindingService.updateAssetsReminding(reminding2);
				}else{
					//更新时间
					TAssetsReminding reminding3 = new TAssetsReminding();
					reminding3.setHandleTime(cal.getTime().getTime());
					reminding3.setId(newAssetsReminding.getId());
					assetsRemindingService.updateAssetsReminding(reminding3);
				}
			}
		}
	}
}
